package concurrent;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @File Info: leetcode -- <ProducerConsumerModel>
 * @Author: 18362
 * @Create: 2022-03-13 18:43:25 星期日
 */
public class ProducerConsumerModel {

    public static ExecutorService threadPool = Executors.newFixedThreadPool(2);
    public static ReentrantLock lock = new ReentrantLock();
    public static Condition notEmpty = lock.newCondition();
    public static Condition notFull = lock.newCondition();
    public static Queue<String> queue = new LinkedList<>();
    public static final int capacity = 10;

    static class ProducerTask implements Runnable {
        private int cnt = 0;
        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    while (queue.size() >= capacity)
                        notFull.await();
                    String product = "Product-" + cnt;
                    cnt++;
                    queue.offer(product);
                    System.out.println("Produce Product: " + product);
                    Thread.sleep(200);
                    notEmpty.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    static class ConsumerTask implements Runnable {
        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    while (queue.size() == 0)
                        notEmpty.await();
                    String product = queue.poll();
                    System.out.println("Consume Product: " + product);
                    Thread.sleep(200);
                    notFull.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }
    }

    public static void main(String[] args) {
        threadPool.submit(new ConsumerTask());
        threadPool.submit(new ProducerTask());
    }

    /*
    private static ReentrantLock lock = new ReentrantLock();
    private static Condition notFull = lock.newCondition();
    private static Condition notEmpty = lock.newCondition();

    private static Queue<String> queue = new LinkedList<>();
    private static final int size = 10;                         // 队列长度

    public static void main(String[] args) {
        Thread producer = new Thread(() -> {
            int i = 0;
            while (true) {
                lock.lock();
                try {
                    while (queue.size() == size) {
                        System.out.println("The queue is full, awaiting!");
                        notFull.await();
                    }
                    Thread.sleep(20);
                    System.out.println("Produce product: " + "product" + i);
                    queue.offer("product" + i);
                    ++i;
                    notEmpty.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }, "producer");
        Thread consumer = new Thread(() -> {
            while (true) {
                lock.lock();
                try {
                    while(queue.isEmpty()) {
                        System.out.println("The queue is empty, awaiting!");
                        notEmpty.await();
                    }
                    Thread.sleep(20);
                    String product = queue.poll();
                    System.out.println("Consume product: " + product);
                    notFull.signalAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        }, "consumer");

        producer.start();
        consumer.start();
    }*/
}
